/* GSMonthInfoDao.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   wzh	2010-5-18	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2010
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hitrust.gs.bean.GSProductApepBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;


public class GSMonthInfoDao {
    public boolean selectMonthInfoByMonth(String month,String warehouse_code)  {
        String sql = "select * from t_month_info where  warehouse_code like '"+warehouse_code+"%' and  months= '"+month+"' and  balance_status='1'";

        DBConnection con = null;
        DBResult dbrs = null;
        ResultSet rs = null;
        boolean flag = true ;
        try {
            con = DBConnection.open(false);
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            while( rs.next()){
                  flag =false;
            }           
            return flag;          
        }

        catch (Exception e) {
            GSLogWriter.debug(this, e);
            GSLogWriter.error(this, e.getMessage());
            e.printStackTrace();
            return false;
        }finally{
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (dbrs != null)
                    dbrs.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * 获取月结时的开始时间和结束时间
     * @param month
     * @param warehouse_code
     * @return
     */
    public List selectMonthInfoTime(String month,String warehouse_code)  {
        String sql = "select start_time,end_time from t_month_info where  warehouse_code='"+warehouse_code+"' and  months= '"+month+"' and balance_status='1'";

        DBConnection con = null;
        DBResult dbrs = null;
        ResultSet rs = null;
        List ls = new ArrayList();
        try {
            con = DBConnection.open(false);
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            while(rs.next()){
                ls.add(rs.getString("start_time"));
                ls.add(rs.getString("end_time"));
            }
            return  ls;
        }
        catch (Exception e) {
            GSLogWriter.debug(this, e);
            GSLogWriter.error(this, e.getMessage());
            return null;
        }finally{
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (dbrs != null)
                    dbrs.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    
    /**
     * @param month
     * @param warehouse_code
     * @return
     */
    public List selectProductDetails(String warehouse_code,String provider_code,String start,String end)  {
        String sql = "select outsub.product_code,pro.apep_code,pro.name,pro.spec,pro.model,pro.product_brand,";
        sql+="pro.product_unit,pro.unit_price,outsub.product_count,outsub.product_money,outsub.provider_code,pro.appender_property";
        sql+=" from t_out_sub outsub left outer join t_out_head outhead on outsub.out_bill_code=outhead.out_bill_code ";
        sql+=" left join t_product pro on pro.id=outsub.product_id where outhead.warehouse_code like '"+warehouse_code+"%' ";
        sql+=" and outsub.provider_code='"+provider_code+"' and outsub.status=1 and outhead.out_type='2003'";
        sql+= " and outhead.out_time >= '" + start + "' ";
        sql+= " and outhead.out_time <= '" + end + "' ";
        sql+=" group by outsub.product_code,pro.apep_code,pro.name,pro.spec,pro.model,pro.product_brand,";
        sql+=" pro.product_unit,pro.unit_price,outsub.product_count,outsub.product_money,outsub.provider_code,pro.appender_property";
        DBConnection con = null;
        DBResult dbrs = null;
        ResultSet rs = null;
        List ls = new ArrayList();
        try {
            con = DBConnection.open(false);
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            while(rs.next()){
            	GSProductApepBean vo=new GSProductApepBean();
            	vo.setConcentricity_code(rs.getString("product_code"));
            	vo.setApep_code(rs.getString("apep_code"));
             	vo.setName(rs.getString("name"));
            	vo.setSpec(rs.getString("spec"));
            	vo.setModel(rs.getString("model"));
               	vo.setProduct_brand(rs.getString("product_brand"));
            	vo.setProduct_unit(rs.getString("product_unit"));
              	vo.setBalance_mode(rs.getString("unit_price"));
             	vo.setManufaturer(rs.getString("product_count"));
              	vo.setArea(rs.getString("product_money"));
             	vo.setProvider_code(rs.getString("provider_code"));
            	vo.setAdapt_machine_type(rs.getString("appender_property"));
                ls.add(vo);
            }
            return  ls;
        }
        catch (Exception e) {
            GSLogWriter.debug(this, e);
            GSLogWriter.error(this, e.getMessage());
            return null;
        }finally{
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (dbrs != null)
                    dbrs.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    
    
    /**
     * 获取月结时的开始时间和结束时间
     * @param month
     * @param warehouse_code
     * @return
     */
    public List selectMonthInfoTimeForServiceCompany(String month,String warehouse_code)  {
        String sql = "select start_time,end_time from t_month_info where  warehouse_code like '"+warehouse_code+"%' and  months= '"+month+"' and balance_status='1'";

        DBConnection con = null;
        DBResult dbrs = null;
        ResultSet rs = null;
        List ls = new ArrayList();
        try {
            con = DBConnection.open(false);
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            while(rs.next()){
                ls.add(rs.getString("start_time"));
                ls.add(rs.getString("end_time"));
            }
            return  ls;
        }
        catch (Exception e) {
            GSLogWriter.debug(this, e);
            GSLogWriter.error(this, e.getMessage());
            return null;
        }finally{
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (dbrs != null)
                    dbrs.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
